import { useI18n } from "vue-i18n";
import { ref, reactive } from "@vue/reactivity";
import { getUserInfo } from "@/api/user";
import { Toast } from "vant";
export default function () {
  var { t } = useI18n();
  var password = ref(null);
  var loginPassValue = ref(null);
  const validatorPassword = (val) => {
    password.value = val;
    //只能是6位数字的--付款密码
    if (!val) {
      return t("password.nummFormat");
    }
    let reg = /^\d{6}$/;
    if (!reg.test(val)) {
      return t("password.sixPassErr");
    }
  };
  const validatorAgain = (val) => {
    if (!val) {
      //只能是6位数字的--确认付款密码
      return t("password.nummFormat");
    }
    let reg = /^\d{6}$/;
    if (!reg.test(val)) {
      return t("password.diffMsg");
    } else if (val !== password.value) {
      return t("password.diffMsg");
    }
  };
  const numberTel = (value, num)=>{
    var result = "";
    let end=value.length-num
 if (value) {

  result = value.substring(0, 3) + "****" + value.substring(end);
  
 }
 return result;

  };
  const loginPass = (val) => {
    //密码正则校验不能为中文，长度校验
    loginPassValue.value = val;
    if (!val) {
      return t("login.passplacehold");
    } else if (val.length < 6 || val.length > 18) {
      return t("login.passLength");
    }
    let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    if (reg.test(val)) {
      return t("login.passRrr");
    }
  };
  const loginPassAgain = (val) => {
    //确认登录密码
    if (!val) {
      return t("login.againpassmsg");
    } else if (val.length < 6 || val.length > 18) {
      return t("login.passLength");
    }
    let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    if (reg.test(val)) {
      return t("login.passRrr");
    } else if (val != loginPassValue.value) {
      return t("password.diffMsg");
    }
  };
        //10-11whatscode

  function onClickLeft() {
    history.back();
  }
  async function updataInfo() {

    return getUserInfo().then((res) => {
       console.log(res)
    if (res.code == 200) {
       localStorage.removeItem("user");
      localStorage.setItem("user", JSON.stringify(res.data));
      userInfo=JSON.parse(localStorage.getItem("user"))
      console.log('userInfo',userInfo)
    }
       if (res.msg) {
      Toast(res.msg)
    }
      
 })

  }
  const emailVaildator = (val) => {
    //邮箱验证
    let bol = /^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (!val) {
      return t("login.emailmsg");
    } else if (!bol.test(val)) {
      return t("login.emerrmsg");
    }
  };
          //邮箱隐藏中间字符
   const numberEmail=(email)=> {
        if (String (email).indexOf ('@') > 0) {
        let newEmail, str = email.split('@'), _s = '';

        if (str[0].length > 4) {
            _s = str[0].substr (0, 4);
            for (let i = 0; i < str[0].length - 4; i++) {
                _s += '*';
            }
        } else {
            _s = str[0].substr (0, 1);
            for (let i = 0; i < str[0].length - 1; i++) {
                _s += '*';
            }
        }
          newEmail = _s + '@' + str[1];
          console.log(newEmail)
        return newEmail;
        } else {
               console.log(email)
        return email;
    }
  };
  var userInfo = JSON.parse(localStorage.getItem("user")); //用户信息
  return {
    validatorPassword,
    validatorAgain,
    numberTel,
    loginPass,
    userInfo,
    onClickLeft,
    loginPassAgain,
    updataInfo,
    emailVaildator,
    numberEmail
  };
}
